package com.leetcode.offer.chapter5;

import com.leetcode.basic.TreeNode;

/**
 * @author Dennis Li
 * @date 2020/7/17 20:48
 */
public class IsBalanced_55 {

    // 不仅要判断根节点，还要判断左右子树是否满足条件
    public boolean isBalanced(TreeNode root) {
        return root == null || (Math.abs(maxDepth(root.left) - maxDepth(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right));
    }

    public int maxDepth(TreeNode root) {
        return root == null ? 0 : 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
    }

}
